package com.kehutong.work.order.manage.app;

import java.util.Arrays;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.util.Objects;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.work.order.manage.entity.WorkOrderManage;
import com.kehutong.work.order.manage.service.WorkOrderClassifyService;
import com.kehutong.work.order.manage.service.WorkOrderManageService;
import com.kehutong.work.order.manage.service.WorkOrderSettingService;
import com.kehutong.work.order.manage.service.WorkOrderTypeService;

/**
 * 企微端-工单
 */
@MvcAction
@ReqMapping("/kht-bin/work/order/manage")
public class WorkOrderAction {

    @Inject
    private WorkOrderManageService manageService;
    @Inject
    private WorkOrderTypeService typeService;
    @Inject
    private WorkOrderClassifyService classifyService;
    @Inject
    private WorkOrderSettingService settingService;

    @ReqMapping("/list/page")
    public Object listPage(Token token, JSONObject jsonObject) {
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return Result.success(manageService.listAppPage(jsonObject, token.getUuid()));
    }

    @ReqMapping("/search")
    public Object search(Token token, JSONObject jsonObject){
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return manageService.search(token.getUuid(), jsonObject);
    }

    @ReqMapping("/assign")
    public Object assign(Token token, JSONObject jsonObject){
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        String id = jsonObject.getString("id");
        WorkOrderManage order = Pool.get(WorkOrderManage.class, id);
        if (Objects.isNull(order)) {
            return org.coraframework.mvc.http.result.Result.error(0, "不存在该工单");
        }

        String handleEmployeeNo = jsonObject.getString("handleEmployeeNo");
        String handleEmployeeName = jsonObject.getString("handleEmployeeName");
        order.setHandleEmployeeNo(Arrays.asList(handleEmployeeNo));
        order.setHandleEmployeeName(Arrays.asList(handleEmployeeName));
        return manageService.assign(token, jsonObject, order);
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(Token token, WorkOrderManage manage) {
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return manageService.save(manage, token, false);
    }

    @ReqMapping("/type/list")
    public Object typeList(JSONObject jsonObject) {
        return typeService.list(jsonObject);
    }

    @ReqMapping("/classify/list")
    public Object classifyList(JSONObject jsonObject) {
        return classifyService.list(jsonObject);
    }

    @ReqMapping("/handle/order")
    public Object handleOrder(Token token, JSONObject jsonObject){
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return manageService.handleWorkOrder(token.getUuid(), token.getUsername(), jsonObject, false);
    }

    @ReqMapping("/detail")
    public Object detail(Token token, String id) {
        return manageService.detail(token, id, false);
    }

    @ReqMapping("/delete")
    public Object delete(Token token, WorkOrderManage manage) {
        return manageService.delete(manage, token.getUuid());
    }

    @ReqMapping("/sign/in/list")
    public Object signInList(JSONObject jsonObject) {
        return manageService.signInList(jsonObject);
    }

    @ReqMapping("/sign/in/detail")
    public Object signInDetail(String id) {
        return manageService.signInDetail(id);
    }

    @ReqMapping("/reminders")
    public Object reminders(String id) {
        return manageService.reminders(id);
    }

    @ReqMapping("/get/setting")
    public Object getSetting() {
        return Result.success(settingService.getSetting());
    }

    private boolean checkLogin(Token token){
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }
}
